package com.sew.sewweb.common.utils;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;



import java.io.IOException;
import java.util.Map;

public class HttpUtils {
    /**
     * http get请求
     * @param url
     * @param token
     * @return
     */
    public static String executeGet(String url, String token) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);
        request.setHeader("Authorization",token);
        request.setHeader("Content-Type","application/json;charset=UTF-8");
        String result=null;
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            result= EntityUtils.toString(entity,"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * http 获取token
     * @param
     * @param
     * @return
     */
    public static String getAeccessToken() {
        String url="https://jcjtgkpt.bmrb.com.cn/ejc-idm-web/auth/getAccessToken?appId=1539077425646063618&secret=MTUzOTA3NzQyNTY0NjA2MzYxOA==";
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);
        String result=null;
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            result= EntityUtils.toString(entity,"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSON json = JSONUtil.parse(result);
        Map<String,Object> data = (Map) json.getByPath("data");
        String token=data.get("access_token").toString();
        return token;
    }
}
